//
//  OPEatDetailWithImgCell.m
//  OnePiece
//
//  Created by wsy on 15/10/28.
//  Copyright © 2015年 wsy. All rights reserved.
//

#import "OPEatDetailWithImgCell.h"
#import <UIImageView+WebCache.h>
@implementation OPEatDetailWithImgCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:1.000 green:0.985 blue:0.971 alpha:1.000];
        _picture = [[UIImageView alloc]init];
        [self.contentView addSubview:_picture];
        _desc = [[UILabel alloc]init];
        _desc.font = [UIFont systemFontOfSize:14];
        _desc.numberOfLines = 0;
        [self.contentView addSubview:_desc];
    }
    return  self;
}

- (void)setConfigureModel:(OPEatDetailArrayModel *)configureModel
{
    [_picture sd_setImageWithURL:[NSURL URLWithString:configureModel.value] placeholderImage:[UIImage imageNamed:@"beijingImg.jpg"]];
    _picture.frame = CGRectMake(10, 0, SCREEN_WIDTH-20, [OPEatDetailWithImgCell heightOfPicture:configureModel.value]);
    _desc.text = configureModel.ext[@"desc"];
    _desc.textColor = [UIColor colorWithWhite:0.221 alpha:1.000];
    _desc.frame = CGRectMake(20, [OPEatDetailWithImgCell heightOfPicture:configureModel.value]+5, SCREEN_WIDTH-40, [OPEatDetailWithImgCell heightOfString:configureModel.ext[@"desc"]]);
}
+ (CGFloat)heightOfPicture:(NSString *)str
{
    UIImageView *img = [[UIImageView alloc]init];
    [img sd_setImageWithURL:[NSURL URLWithString:str]];
    CGFloat height = 0;
    if (img.image.size.height != 0) {
        height = (SCREEN_WIDTH-20)*img.image.size.height/img.image.size.width;
    }
    return height;
}

+ (CGFloat)heightOfString:(NSString *)str
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName];
    CGRect bounds = [str boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-40, 0) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    return bounds.size.height;
}

+ (CGFloat)heightForCell:(OPEatDetailArrayModel *)model
{
    return [OPEatDetailWithImgCell heightOfPicture:model.value]+10+[OPEatDetailWithImgCell heightOfString:model.ext[@"desc"]];
}

@end
